#!/bin/bash
#===============================================================================
#          FILE:  AKM
# 
#         USAGE:  sudo ./AKM 
# 
#   DESCRIPTION:  Automatically un-tars, compiles, and installs a kernel
#
#  REQUIREMENTS:  See README
#        AUTHOR:  Anthony Byrne, abyrne561@gmail.com
#       VERSION:  1.2
#       CREATED:  09/21/2009 04:50:38 PM EDT
#       LICENSE:  GPL
#===============================================================================
################################################################################
# Auto Linux Kernel Maker (AKM) 
# Copyright 2009 Anthony Byrne
#
# AKM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AKM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AKM.  If not, see <http://www.gnu.org/licenses/>.
################################################################################
# Make sure your in the folder where a .tar.bz2 downloaded from kernel.org
# MAKE SURE YOUR ROOT WHEN RUNNING THIS!

# Show error if not root
if [ $USER != root ]; then
  zenity --error --text="Must Be Root"
  exit 0
fi

( #Start LOG
# Warning
zenity --info --text="Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program, don't just hit cancel!"

# Ask and fill variables
KERNEL_VER="$(zenity --entry \ 
                     --text="What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!")"

# Decompress
tar xivf linux-${KERNEL_VER}.tar.bz2 | zenity --progress \ 
                                              --auto-close \
                                              --pulsate \
                                              --text="Decompressing Kernel"

# Go into Kernel source
cd linux-$KERNEL_VER

# Choose a Method
METHOD="$(zenity --height=200 \
                 --width=410 \
                 --title "Configuration" \
                 --text "Choose a method for configuration" \
                 --list \
                 --radiolist \
                 --column "Select" --column "Method" \ 
                 True "Configure using the GUI (requires qt3-examples)" \
                 False "Configure using the Terminal" False "Use a pre-existing config file")"

if [[ "$METHOD" == "Configure using the GUI (requires qtexamples)" ]]; then
make xconfig  
fi

if [[ "$METHOD" = "Configure using the Terminal" ]]; then
xterm -e make config  
fi

if [[ "$METHOD" = "Use a pre-existing config file" ]]; then
zenity --info \
       --text="You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config"
xterm -e make oldconfig  
fi

( #START LOGGING
# Compile the kernel (takes a long time)
echo "5"
make 
echo "50"

# Install the kernel modules
echo "# Installing Kernel Modules"
make modules_install 
echo "76"

# Install the kernel
echo "# Installing Kernel"
make install 
echo "87"

# Crestes a new Initial RAM File System (initramfs)
echo "# Running Post-Installation Tasks (Generating INITRAMFS)" 
update-initramfs -k ${KERNEL_VER} -c
echo "95"

# Updates Bootloader
echo "# Running Post-Installation Tasks (Updating GRUB)"
update-grub
echo "100"
) | 
zenity --progress \
          --title="Kernel Compiler" \
          --text="Compiling Kernel (This takes a really long time!)" \
          --auto-close \
          --percentage=0

cd ..

# Clean up process
DELETE="$(zenity \
          --height=200 \
          --width=410 \
          --title "Clean up?" \
          --text "Do you want to clean up?" \
          --list \
          --radiolist \
          --column "Select" --column "Method" \
          True "No, Leave everything." \
          False "Yes, just delete the un-needed files for me." \
          False "Yes, but keep the .config file for the future."
          )"

if [[ "$DELETE" == "Yes, just delete the un-needed files for me." ]]; then
rm -r -f linux-$KERNEL_VER
fi

if [[ "$DELETE" == "Yes, but keep the .config file for the future." ]]; then
cp /boot/config-${KERNEL_VER} -t ~/
rm -r -f linux-$KERNEL_VER
zenity --info \
       --text="If you want to use the config file from this kernel in the furture, it is located in your home folder under the name of config-${KERNEL_VER}"
sleep 2
fi

# ALL DONE
zenity --info \
       --text="All Done! Your new kernel has been installed!"
) 2>&1 | tee AKM.log
